package com.htdc.web.controller;

import com.htdc.web.bean.Company;
import com.htdc.web.bean.ProjectTb;
import com.htdc.web.service.CountService;
import com.htdc.web.service.ProjectService;
import com.htdc.web.utils.DateUtils;
import com.htdc.web.utils.ExcelParam;
import com.htdc.web.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

/**
 * author:Administrator
 * date:2018/9/5
 * description:
 */
@Controller
@RequestMapping("/count")
public class CountController {

    @Autowired
    private CountService countService;
    @Autowired
    private ProjectService projectService;

    //项目列表
    @RequestMapping("/getProject")
    public String getProject(int cid,HttpSession session){
        List<Company> companies=countService.getAllCompanyData();
        List<ProjectTb> project = null;
        if(cid==0) {
            project= countService.getProject();
        }else{
            project = projectService.getProjectByCid(cid);
        }
        session.setAttribute("companies",companies);
        session.setAttribute("project",project);
        return "project";
    }

    //列表
    @RequestMapping(value = "/getData",method = RequestMethod.GET)
    public String getData(int pid,HttpSession session){
        List<ProjectTb> project =countService.getProject();
        ProjectTb projectTb = countService.getData(pid);
        session.setAttribute("project",project);
        session.setAttribute("projectTb",projectTb);
        return "data_project";
    }

    //租凭合同（公司）
    @RequestMapping("/getAllCompanyData")
    public String getAllCompanyData(HttpSession session){
        List<Company> companies = countService.getAllCompanyData();
        session.setAttribute("companies",companies);
        return "rent_data_company";
    }


    //租凭合同（合同）
    @RequestMapping("/getAllProjectData")
    public String getAllProjectData(HttpSession session){
        List<ProjectTb> projectTbsp =countService.getAllProjectData();
        session.setAttribute("projectTbsp",projectTbsp);
        return  "rent_data_project";
    }

    //租凭合同公司数据
    @RequestMapping(value = "/getCompanyData",method = RequestMethod.GET)
    public String getCompanyData(int cid, HttpSession session){
        Company company = countService.getCompanyData(cid);
        session.setAttribute("company",company);
        return "count_company";
    }

    //租凭合同项目数据
    @RequestMapping(value = "/getProjectData",method = RequestMethod.GET)
    public String getProjectData(int pid,HttpSession session){
        List<ProjectTb> projects = countService.getProjectData(pid);
        session.setAttribute("projects",projects);
        return "count_project";
    }

    //公司导出
    @RequestMapping(value = "/downloadCompany")
    public void downloadCompany(HttpSession session,HttpServletResponse response) throws Exception {
        List<Company> companies = (List<Company>) session.getAttribute("companies");
        String[] heads = {"编号","公司名称", "可租面积", "实际面积", "当前租金总额", "实际租金总额", "收缴率"};
        List<String[]> data = new LinkedList<>();
        for (int i = 0; i < companies.size(); i++) {
            Company company = companies.get(i);
            String[] temp = new String[10];
            temp[0] = company.getCid()+"";
            temp[1] = company.getCname();
            temp[2] = company.getTotalRealArea();
            temp[3] = company.getTotalRealRentArea();
            temp[4] = company.getCTotalMoney();
            temp[5] = company.getPTotalRealMoeny();
            temp[6] = Double.parseDouble(company.getCTotalMoney())/Double.parseDouble(company.getPTotalRealMoeny())+"";
            data.add(temp);
        }
        ExcelParam param = new ExcelParam.Builder("租赁数据[公司]").headers(heads).data(data).build();
        ExcelUtil.export(param, response);
    }

    //项目导出
    @RequestMapping(value = "/downloadProject")
    public void downloadProject(HttpServletResponse response) throws Exception {

        List<ProjectTb> projects = countService.getProject();
        String[] heads = {"编号","项目名称", "可租面积", "实际面积", "当前租金总额", "实际租金总额", "收缴率"};
        List<String[]> data = new LinkedList<>();
        for (int i = 0; i < projects.size(); i++) {
            ProjectTb project= projects.get(i);
            String[] temp = new String[10];
            temp[0] = project.getPid()+"";
            temp[1] = project.getPname();
            temp[2] = project.getTotalRealArea()+"";
            temp[3] = project.getTotalRealRentArea()+"";
            temp[4] = project.getPTotalMoney()+"";
            temp[5] = project.getPTotalRealMoeny()+"";
            try{
                temp[6] = project.getPTotalRealMoeny()/project.getPTotalRealMoeny()+"";
            }catch (Exception e){
                temp[6]="100";
            }
                data.add(temp);
        }
        ExcelParam param = new ExcelParam.Builder("租赁数据[项目]").headers(heads).data(data).build();
        ExcelUtil.export(param, response);
    }
}
